package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.BuyService;
import com.itwn.comic.vo.BuyDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("buyDetail")
public class BuyDetailController {

    @Autowired
    BuyService buyService;

    @GetMapping("/initBuyDetail")
    public ResponseResult<PageInfo<BuyDetailVo>> initBuyDetailList(@RequestParam(value = "now" , defaultValue = "1") Integer now ,
                                                                @RequestParam(value = "size" , defaultValue = "3") Integer size ,
                                                                String username ,
                                                                String resourcename){
        ResponseResult<PageInfo<BuyDetailVo>> result = new ResponseResult<>() ;
        try {
            PageInfo<BuyDetailVo> pageInfo = buyService.queryDetailByPages(now , size , username , resourcename) ;
            result.setCode(200);
            result.setMsg("恭喜你,查询成功");
            result.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,查询失败");
        }
        return result ;
    }

    @RequestMapping("/removeDetail")
    public ResponseResult<Void> removeByKey( Integer userId , Integer sectionId){
        ResponseResult<Void> result = new ResponseResult<>() ;

        try {
            buyService.removeByKey(userId , sectionId) ;
            result.setCode(200);
            result.setMsg("恭喜你,删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,删除失败");
        }

        return result ;
    }
}
